package com.ld.security.shop.crawler.modules.wo.api;

import cn.hutool.core.lang.Console;
import com.ld.security.common.core.base.vo.DataVo;
import com.ld.security.common.mybatis.base.po.IdPo;
import com.ld.security.common.tomcat.base.api.*;

import com.ld.security.shop.crawler.modules.wo.constant.WoIpConstant;
import com.ld.security.shop.crawler.modules.wo.entity.WoIp;

import com.ld.security.shop.crawler.modules.wo.service.IWoIpService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(tags="代理ipApi管理")
@RequestMapping("/api/woIp")
@Slf4j
public class  WoIpApi extends  SuperApi<WoIp> {

    @Autowired
    IWoIpService iWoIpService;

    @RequestMapping(value = "/init",method = RequestMethod.POST)
    @ApiOperation("初始化")
    public DataVo initWebClient(){
       int total =  iWoIpService.init();
        DataVo vo =  DataVo.of();
        vo.setMessage("成功初始化"+total+"个客户端");
        return vo;
    }

    @Scheduled(cron = "0/30 * * * * ?")
    public void init(){
        log.info("客户端初始化");
        WoIp q =  this.newInstance();
        q.setStatus(WoIpConstant.status.normal);
        long x = iWoIpService.getWebClients().size();

        synchronized (this.getClass()){
            long y = iWoIpService.count(q);
            if( x != y){
                DataVo vo = this.initWebClient();
                log.info(vo.getMessage());

            }
        }

    }


}
